package com.iqiyi.pps.epg.core.service.push;

import com.iqiyi.pps.epg.core.dao.push.AutoPushMsgDao;
import com.iqiyi.pps.epg.core.model.program.ProgramData;
import com.iqiyi.pps.epg.core.model.push.AutoPushMsg;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by zhang on 2016/1/27.
 */
@Service
@Transactional
public class AutoPushMsgServiceImpl implements AutoPushMsgService {
    private static final Logger logger = LoggerFactory.getLogger(AutoPushMsgServiceImpl.class);
    @Autowired
    private AutoPushMsgDao dao;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void save(AutoPushMsg entity) {
        if (entity == null)
            return;
        long startTime = System.currentTimeMillis();
        /*保存 记录 */
        Session session = dao.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        try {
            session.save(entity);
            transaction.commit();
        } catch (Exception e) {
            logger.error(e.getMessage());
            transaction.rollback();
        } finally {
            session.close();
            logger.info("[AutoPushMsgDao.save()][cost={}]", System.currentTimeMillis() - startTime);
        }

    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void saveList(List<AutoPushMsg> entityList) {
        if (entityList == null || entityList.size() <= 0)
            return;
        long startTime = System.currentTimeMillis();
        /*保存 记录 */
        dao.saveBatchJDBC(entityList);
        logger.info("[AutoPushMsgDao.saveList()][cost={}]", System.currentTimeMillis() - startTime);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public boolean find(Long episodeId) {
        if (episodeId == null && episodeId <= 0) {
            return false;
        }
        return dao.find(episodeId);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public boolean findEffective(Long episodeId, Date currentTime) {
        if (episodeId == null || episodeId <= 0 || currentTime == null) {
            return false;
        }
        long startTime = System.currentTimeMillis();
        boolean result = dao.findEffective(episodeId,currentTime);
        logger.info("[AutoPushMsgDao.findEffective()][cost={}]", System.currentTimeMillis() - startTime);
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<AutoPushMsg> getPushMsg(long startTime, long endTime, int start, int max) {
        if (startTime <= 0 || endTime < startTime || start < 0 || max < 0) {
            return null;
        }
        return dao.getPushMsg(startTime, endTime, start, max);
    }

    @Override
    public int updatePushStatusByPushTime(long startTime, long endTime, int updateStatus) {
        if (startTime <= 0 || endTime < startTime) {
            return  -1;
        }
        return dao.updatePushStatusByPushTime(startTime, endTime, updateStatus);
    }

    @Override
    public int updatePushStatusByIds(String ids, int updateStatus) {
        if (StringUtils.isNotEmpty(ids)) {
            return 0;
        }
        if (ids.endsWith(",")) {
            ids = ids.substring(0, ids.length() - 1);
        }
        long startTime = System.currentTimeMillis();
        int result = dao.updateJDBCPushStatusByIds(ids, updateStatus);
        logger.info("[AutoPushMsgDao.updatePushStatusByIds()][cost={}]", System.currentTimeMillis() - startTime);
        return result;
    }

    @Override
    public int updatePushByEpisodeId(ProgramData programData) {
        if (programData == null) {
            return 0;
        }
        long currentTime = System.currentTimeMillis();
        long noticeStartTime = programData.getNoticeStartTime().getTime();
        if (noticeStartTime - currentTime < 30 * 60 * 1000) {
            long iosPushTime = noticeStartTime - AutoPushMsg.IOS_PUSH_TIME;
            long andPushTime = noticeStartTime - AutoPushMsg.ANDRIOD_PUSH_TIME;
            int iosUpdateResult = dao.updatePushTimeByEpisodeId(programData.getId(), AutoPushMsg.AUTO_PUSH_TYPE_IOS, iosPushTime);
            int andUpateResult = dao.updatePushTimeByEpisodeId(programData.getId(), AutoPushMsg.AUTO_PUSH_TYPE_ANDRIOD, andPushTime);
            return iosUpdateResult + andUpateResult;
        } else {
            return dao.updatePushStatusByEpisodeId(programData.getId(), -1);
        }
    }

    @Override
    public int updatePushMsgById(String ids, AutoPushMsg autoPushMsg) {
        if (StringUtils.isEmpty(ids) || autoPushMsg == null) {
            return -1;
        }
        if (ids.endsWith(",")) {
            ids = ids.substring(0, ids.length() - 1);
        }
        return dao.updatePushMsgById(ids, autoPushMsg);
    }
}
